<template>
  <div style="padding: 10px">
    <div>
      <el-button type="danger" @click="handleBatchDel" plain
        >批量删除</el-button
      >
    </div>
    <el-table
      :data="list"
      border
      stripe
      style="width: 100%"
      @selection-change="handleSelection"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column type="index" label="序号" width="80"> </el-table-column>
      <el-table-column prop="borrowAmount" label="借款额度"> </el-table-column>
      <el-table-column prop="integralStart" label="积分区间开始">
      </el-table-column>
      <el-table-column prop="integralEnd" label="积分区间结束">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: center; margin-top: 30px">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[2, 3, 4, 5, 6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>
<script>
import integralGradeApi from "@/api/integralGrade";
export default {
  data() {
    return {
      list: [], //页面需要展示的数据集合
      pageNum: 1,
      pageSize: 3,
      total: 0,
      chooseData: [],
    };
  },
  created() {
    //页面初始化时 查询第一页的分页数据
    this.getPage();
  },
  methods: {
    //批量删除按钮事件函数
    handleBatchDel() {
      // console.log("批量删除:", this.chooseData);
      //1、判断如果没有选中要删除的数据   给用户警告
      if (this.chooseData.length === 0) {
        this.$message.warning("请选中要删除的积分等级");
        return;
      }
      //2、如果选中了积分等级，弹出删除确认提示框
      this.$confirm("此操作将永久删除积分等级, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //3、确认删除 将选中的积分等级的id获取存到一个数组中
          let ids = [];
          this.chooseData.forEach((item) => {
            // console.log(item.id);
            ids.push(item.id); //
          });
          //4、发起批量删除的请求
          integralGradeApi.batchDel(ids).then((r) => {
            //删除成功
            this.$message.success("批量删除成功");
            //刷新页面
            this.getPage();
          });
        })
        .catch(() => {
          //取消回调
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //表格行选中状态改变的回调
    handleSelection(selection) {
      console.log(selection);
      this.chooseData = selection;
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPage();
    },
    handleCurrentChange(num) {
      this.pageNum = num;
      this.getPage();
    },
    //编辑按钮单击事件
    handleEdit(index, row) {
      // console.log(row);
      //跳转到更新积分等级表单页
      //1、请求参数传参
      // this.$router.push(`/integral-grade/edit?id=${row.id}`); //通过路由对象 在路由文件中匹配路由路径 成功后跳转到指定的页面
      //2、路径参数传参
      this.$router.push(`/integral-grade/edit/${row.id}`);
    },
    //删除按钮单击事件
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该积分等级, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //确认回调
          integralGradeApi.delById(row.id).then((r) => {
            //弹出删除成功的提示
            this.$message.success("删除成功");
            //刷新页面数据
            this.getPage();
          });
        })
        .catch(() => {
          //取消回调
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    getPage() {
      integralGradeApi.getPage(this.pageNum, this.pageSize).then((r) => {
        this.list = r.data.page.records;
        this.total = r.data.page.total;
      });
    },
  },
};
</script>
